package com.linq.cool.gateway.handler;

import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.exception.ValidateCodeException;
import com.linq.cool.gateway.service.ValidateCodeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.io.IOException;


/**
 * @Author: yqlin
 * @Date: 2021/1/18 00:01
 * @Description: 图片验证码处理
 * @Version: 1.0.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class ValidateCodeHandler implements HandlerFunction<ServerResponse> {
    private final ValidateCodeService validateCodeService;

    @Override
    public Mono<ServerResponse> handle(ServerRequest serverRequest) {
        Result<?> result = null;
        try {
            result = validateCodeService.createCaptcha();
        } catch (ValidateCodeException | IOException ex) {
            return Mono.error(ex);
        }
        return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(result));
    }

}
